package com.szewec.ps.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by
 */

public class DateUtil {
    public static final String PATTERN = "yyyy-MM-dd HH:mm:ss";

    /**
     * 获取系统时间戳
     *
     * @return
     */
    public static Long getCurTimeInLong() {
        long time = System.currentTimeMillis();
        return time;
    }

    /**
     * 获取当前时间
     *
     * @param pattern
     * @return
     */
    public static String getCurDateInString(String pattern) {
        pattern = StringUtils.isEmpty(pattern) ? PATTERN : pattern;
        SimpleDateFormat sDateFormat = new SimpleDateFormat(pattern);
        return sDateFormat.format(new Date());
    }

    /**
     * 时间戳转换成字符窜
     *
     * @param milSecond
     * @param pattern
     * @return
     */
    public static String convertLongToString(Long milSecond, String pattern) {
        pattern = StringUtils.isEmpty(pattern) ? PATTERN : pattern;
        if (milSecond == null) {
            return "";
        } else {
            Date date = new Date(milSecond);
            SimpleDateFormat format = new SimpleDateFormat(pattern);
            return format.format(date);
        }
    }

    /**
     * 将字符串转为时间戳
     *
     * @param dateString
     * @param pattern
     * @return
     */
    public static Long convertStringToLong(String dateString, String pattern){
        pattern = StringUtils.isEmpty(pattern) ? PATTERN : pattern;
        if(StringUtils.isEmpty(dateString)){
            return null;
        }else {
            Long date = null;
            SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
            try {
                date = dateFormat.parse(dateString).getTime();
            } catch (ParseException e) {
                e.printStackTrace();
            }
            return date;
        }
    }

    /**
     * 时间戳转换成字符窜
     * @param milSecond
     * @param pattern
     * @return
     */
    public static String getDateToString(Long milSecond, String pattern) {
        pattern = StringUtils.isEmpty(pattern) ? PATTERN : pattern;
        if(milSecond == null){
            return "";
        }else{
            Date date = new Date(milSecond);
            SimpleDateFormat format = new SimpleDateFormat(pattern);
            return format.format(date);
        }
    }

    /**
     * 将Date类型转为字符串
     *
     * @param date
     * @param pattern
     * @return
     */
    public static String convertDateToString(Date date, String pattern){
        pattern = StringUtils.isEmpty(pattern) ? PATTERN : pattern;
        if(date == null){
            return null;
        }else {
            SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
            return dateFormat.format(date);
        }
    }

    /**
     * 将Date类型转为字符串
     */
    public static String convertDateToString(Date date) {
        return convertDateToString(date, PATTERN);
    }

    /**
     * 将字符串转为Date类型
     *
     * @param dateString
     * @param pattern
     * @return
     */
    public static Date convertStringToDate(String dateString, String pattern){
        pattern = StringUtils.isEmpty(pattern) ? PATTERN : pattern;
        if(StringUtils.isEmpty(dateString)){
            return null;
        }else {
            Date date = null;
            SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
            try {
                date = dateFormat.parse(dateString);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            return date;
        }
    }

    /**
     * 将字符串转为Date类型 默认格式为yyyy-MM-dd HH:mm:ss
     *
     * @param dateString 日期字符串
     */
    public static Date convertStringToDate(String dateString){
        return convertStringToDate(dateString, PATTERN);
    }
}